#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>

const int N = 1e7 + 5;
const double eps = 1e-9;
const int P = 998244353;

int qpow(int x, int p) {
	int res = 1, base = x;
	while(p) {
		if(p & 1) res = 1ll * res * base % P;
		base = 1ll * base * base % P;
		p >>= 1;
	}
	return res;
}

int n, a[N], cnt[N];
int A, B, C, M;
double p[N][4];
std::pair<int, int> f[4], g[4];

void solve() {
	std::cin >> n >> a[0] >> A >> B >> C >> M;
	for(int i = 1; i <= n; ++i) cnt[i] = 0;
	for(int i = 1; i <= n; ++i) {
		a[i] = (1ll * A * a[i-1] % M * a[i-1] + 1ll * B * a[i-1] + C) % M + 1;
		++cnt[a[i]];
	}
	int m = 0;
	for(int i = 1; i <= n; ++i) if(cnt[i]) {
		int x = cnt[i];
		cnt[++m] = x;
	}
	double l = 0, r = 1;
	double ans = 0;
	while(std::fabs(r - l) > eps) {
		double mid = (l + r) / 2;
		if(cnt[1] > 1) {
			p[1][2] = mid;
			p[1][3] = 1 - mid;
		} else {
			p[1][3] = 1;
		}
		bool ok = 1;
		for(int i = 2; i <= m; ++i) {
			if(cnt[i] > 1) {
				p[i][2] = mid;
			} else {
				p[i][2] = 0;
			}
			p[i][1] = std::max(0.0, 2 * mid - p[i-1][3]);
			p[i][3] = 1 - p[i][1] - p[i][2];
			if(p[i][3] < -eps) {
				ok = 0;
				break;
			}
		}
		if(ok) {
			ans = mid;
			l = mid;
		} else {
			r = mid;
		}
	}
	if(cnt[1] > 1) {
		p[1][2] = ans;
		p[1][3] = 1 - ans;
	} else {
		p[1][3] = 1;
	}
	for(int i = 2; i <= m; ++i) {
		if(cnt[i] > 1) {
			p[i][2] = ans;
		} else {
			p[i][2] = 0;
		}
		p[i][1] = std::max(0.0, 2 * ans - p[i-1][3]);
		p[i][3] = 1 - p[i][1] - p[i][2];
	}
	f[1] = f[2] = f[3] = std::make_pair(0, 0);
	if(cnt[1] > 1) {
		f[2] = std::make_pair(1, 0);
		f[3] = std::make_pair(-1, 1);
	} else {
		f[3] = std::make_pair(0, 1);
	}
	int x = 1, y = 1;
	for(int i = 2; i <= m; ++i) {
		if(cnt[i] > 1) {
			g[2] = std::make_pair(1, 0);
		} else {
			g[2] = std::make_pair(0, 0);
		}
		if(2 * ans - p[i-1][3] < eps) {
			g[1] = std::make_pair(0, 0);
			if(2 - f[3].first > 0) {
				assert(f[3].second > 0);
				int x0 = f[3].second, y0 = 2 - f[3].first;
				if(1ll * x0 * y < 1ll * x * y0) {
					x = x0, y = y0;
				}
			}
		} else {
			g[1] = std::make_pair(2 - f[3].first, -f[3].second);
		}
		g[3] = std::make_pair(-g[1].first - g[2].first, 1 - g[1].second - g[2].second);
		f[1] = g[1], f[2] = g[2], f[3] = g[3];
		//ans * f[3].first >= -f[3].second
		if(f[3].first < 0) {
			assert(f[3].second > 0);
			int x0 = f[3].second, y0 = -f[3].first;
			if(1ll * x0 * y < 1ll * x * y0) {
				x = x0, y = y0;
			}
		}	
	}
	//std::cerr << x << "/" << y << "\n";
	std::cout << std::setprecision(10) << ans << " " << 1ll * x * qpow(y, P - 2) % P << "\n";
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	std::freopen("guess.in", "r", stdin);
	std::freopen("guess.out", "w", stdout);

	int t; std::cin >> t;
	while(t--) solve();

	return 0;
}
